/*
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.pps.gl.server;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import org.pps.gl.client.InscriptionService;
import org.pps.gl.server.datastore.Utilisateur;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class InscriptionServiceImpl extends RemoteServiceServlet implements
		InscriptionService {

	private static final long serialVersionUID = 4322036277478395952L;

	/**
	 * Méthode qui insère un nouvel utilisateur dans le datastore
	 * 
	 * @return 0 si l'utilisateur est inséré. 1 si l'user existe.
	 */
	@Override
	public Integer InscrireUtilisateur(String email, String nom, String prenom,
			String password, String dateNaissance, String sexe) {

		if (null != Utilisateur.getUtilisateur(email))
			return 1;
		
		String shaPassword = password;
		try {
			shaPassword = UtilSHA1.SHA1(password);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		Utilisateur.createUser(email, shaPassword, nom, prenom, dateNaissance,
				sexe);
		System.out
				.println(Utilisateur.getUtilisateur(email).getProperty("nom"));

		return 0;
	}
}
